Skip to content

fix(mf4): defer asammdf imports until reader/writer construction - #2123

Merged
zariiii9003 merged 9 commits into
hardbyte:mainfrom
timothyanderson096-ocdealcheck:fix/mf4-import-order-2122
Nov 22, 2026
Merged

zariiii9003 merged 9 commits into
hardbyte:mainfrom
timothyanderson096-ocdealcheck:fix/mf4-import-order-2122

Conversation

@timothyanderson096-ocdealcheck

@timothyanderson096-ocdealcheck timothyanderson096-ocdealcheck commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Current maintenance update — 23 November 2026

Latest head: f02ac6ea4757ed1601d23497c4f31dffab90a3b8. The previous head 398890a passed docs and static analysis but its hosted matrix failed four inherited MF4 reader/writer integration tests on runners without the optional asammdf package. The earlier fix skipped the parametrized payload tests; this revision adds a class-level optional-dependency skip for inherited TestMF4FileFormat integration tests as well. Real MF4 integration tests remain enabled whenever asammdf is installed. No production code changed in these maintenance commits.

Current head requires fresh hosted validation. Historic successful CI for dcbe0e4 and historic local results below do not establish that f02ac6e passes.

Summary of Changes

Importing asammdf first can re-enter can through canmatrix/canopen before asammdf finishes initializing. The optional-import guard then permanently treats an installed dependency as missing.

  • Load asammdf when an MF4 reader or writer is constructed.
  • Report a genuinely missing asammdf package separately from a missing transitive dependency, before opening the output file.
  • Cover native import orders, a deterministic circular import, missing dependencies and file-opening controls.
  • Integrate current main while preserving UTC writer timestamps and the packed error-frame repair from fix(mf4): read packed error-frame payloads without losing byte order #2115. Its eight payload regressions retain their assertions and import helper classes/constants directly from asammdf.
  • Preserve evaluated public path annotations for Sphinx while keeping asammdf signature types behind type checking.

Closes #2122. Thanks to @nehalkpatel for the reproduction and dependency-chain analysis.

Type of Change

  • Bug fix

Checklist

  • Contribution guide followed; tests and changelog fragment included.
  • Focused regressions and applicable local static checks pass.
  • Hosted Tests workflow passes for the current head (pending); historical successful run and local limitations are recorded below.

Current validation — 13 November 2026

Published revision dcbe0e4394b7286aaa4326557b7bc8805348fd4f merges upstream 86e85794f6c3505c988a4acd5d34f5b08f508b17 into the existing PR. Its complete tree c2d30628aaaeff97d88042704a8a4e3e9d018f2f matches the final locally verified tree.

Environment: Linux / CPython 3.12.14; asammdf 8.7.2, canmatrix 1.2, canopen 2.4.1. The shallow checkout used a local setuptools-scm version override for installation.

  • Final focused MF4 suite: 16 passed, 1 existing unsupported-append skip, 6 subtests passed. This includes the eight upstream packed-payload cases.
  • Identical import regressions on unchanged current upstream fail in four reader/writer subcases and the transitive-error case; all pass with this repair.
  • Full final local suite: 529 passed, 99 skipped, 42 subtests passed, 28 failed. Baseline with the same added import tests: 528 passed, 99 skipped, 38 subtests passed, 33 failed. Every remaining candidate failure is an IPv4/IPv6 multicast initialization failure also present on the baseline; there are no new failing test identities.
  • Project-wide Black and Ruff, changed-source Pylint and whitespace checks pass. Mypy targets Python 3.11–3.15 pass all 65 source files in a separate environment without MF4 extras, matching the type-check environment's optional-dependency boundary. With real MF4 dependencies installed, a separate diagnostic retains seven errors versus 16 on the baseline.
  • 39 documentation doctests pass. Strict HTML documentation generation retains one unresolved _thread.allocate_lock reference also present on unchanged upstream. The introduced path-annotation reference warning was repaired; no warning suppression was added.
  • Independent inspection and runtime probes found no material issue; importing can leaves asammdf unloaded, and writer UTC start time and message writing remain valid.

No physical CAN hardware or minimum asammdf 6 validation is claimed. The historical local full-suite and strict HTML results above retain their stated limitations; successful hosted validation below is separate.

Hosted validation — checked 18 November 2026 (Brisbane)

Tests run 43397825677 completed successfully for the same published revision dcbe0e4394b7286aaa4326557b7bc8805348fd4f. All 21 test-matrix jobs across Ubuntu, macOS and Windows (CPython 3.11–3.15 and PyPy 3.11–3.12), static code analysis, documentation, packaging and coverage aggregation succeeded. Release publication was skipped. These hosted results supersede the earlier pending-CI wording; no source change or new local rerun was needed for this status refresh.

Prepared with OpenAI Codex assistance and independent inspection.

Timothy Anderson — Spotted Ridge Engineering / OC Labs
Independent software verification · GDN (Global Decision Network)
timothyanderson096@gmail.com · Engineering evidence

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T15:09:54.382157Z 2c0bb48 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Preserve UTC writer timestamps and packed error-frame decoding while retaining lazy asammdf imports. Adapt the upstream payload tests to use asammdf-owned helpers and keep runtime path annotations compatible with Sphinx.
@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: Request changes (minor)

Thanks for the thorough analysis. The fix is right: moving the asammdf import from module level into the reader/writer constructors breaks the asammdf → canmatrix → canopen → can cycle from #2122. I reproduced the bug on main with the real dependencies (asammdf 8.8.33, canmatrix 1.2, canopen 2.4.1), and this PR fixes it. Telling "asammdf not installed" (NotImplementedError) apart from "asammdf is broken" (the original ModuleNotFoundError) is a good improvement too.

Please change before merging

Trim the subprocess tests. test/mf4_import_test.py starts 9 fresh interpreters and takes about 8 s, which is a lot for one fix. Only the simulated circular import exercises the bug; the plain import can; import asammdf and import asammdf; import can cases already pass on main. One subprocess for the circular case (read and write in the same script), plus the missing-dependency and broken-dependency checks, covers the same behaviour in about a third of the time.

Changelog fragment. Please name it after this PR (2123.fixed.rst), and mention that a broken asammdf install now raises the original ModuleNotFoundError instead of NotImplementedError.

Suggestion: consider keeping the asammdf calls typed

import_module() returns an untyped ModuleType, so mypy can no longer check any asammdf.MDF(...) or asammdf.Signal(...) call. With asammdf installed, four existing type errors (three Signal(attachment=...), one MDF(file)) become invisible instead of being fixed.

The alternative is normal function-level imports, which this repo's ruff/pylint config only accepts with a suppression comment, as in can/broadcastmanager.py:

# pylint: disable-next=import-outside-toplevel,import-error
from asammdf import MDF, Signal, Source  # noqa: PLC0415

That means six such pairs in mf4.py (or one per-file ignore in pyproject.toml), so it's a trade-off between a cleaner lint config and type checking. I'd lean towards typing, but I won't block on it.

@zariiii9003

Copy link
Copy Markdown
Collaborator

Please move the tests into test/logformats_test.py

@zariiii9003 zariiii9003 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@zariiii9003
zariiii9003 merged commit b3d6181 into hardbyte:main Nov 22, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MF4Writer reports "asammdf package was not found" when asammdf is imported before can

2 participants